import { driver, session, auth } from 'neo4j-driver'

var mydriver = ''

function connect(form) {
  console.log(form)
  mydriver = driver(
    form.protocol + '://' + form.host,
    auth.basic(form.username, form.password)
  )
}

function query(cql, res) {
  var i = 0
  var mysession = mydriver.session({ defaultAccessMode: session.READ })
  mysession.run(cql).subscribe({
    onKeys: keys => {
      console.log(keys)
    },
    onNext: record => {
      this.$set(res, i, record[0])
      i++
      console.log(record.get('name'))
    },
    onCompleted: () => {
      session.close() // returns a Promise
    },
    onError: error => {
      console.log(error)
    }
  })
}

export { connect, query }
